package cn.zhx.xa.system.controller;

import cn.zhx.xa.model.dto.role.SysRoleAddOrUpdateDto;
import cn.zhx.xa.model.dto.role.SysRoleSearchDto;
import cn.zhx.xa.model.entity.SysRole;
import cn.zhx.xa.model.vo.common.Result;
import cn.zhx.xa.model.vo.common.ResultCodeEnum;
import cn.zhx.xa.service.annotation.Log;
import cn.zhx.xa.system.service.SysRoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/api/system/role")
@Tag(name = "角色接口")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService ;

    @PostMapping("/auth/findByPage/{pageNum}/{pageSize}")
    @Operation(summary = "角色信息分页查询")
    public Result<Page<SysRole>> findByPage(@RequestBody SysRoleSearchDto sysRoleSearchDto,
                                            @PathVariable(value = "pageNum") Integer pageNum ,
                                            @PathVariable(value = "pageSize") Integer pageSize) {
        return Result.build(sysRoleService.findByPage(sysRoleSearchDto, pageNum , pageSize)  , ResultCodeEnum.SUCCESS) ;
    }

    @PostMapping(value = "/auth/saveSysRole")
    @Log(title = "角色添加",businessType = 0) //添加Log注解，设置属性
    @Operation(summary = "添加角色")
    public Result saveSysRole(@RequestBody SysRoleAddOrUpdateDto sysRoleAddDto) {
        sysRoleService.saveSysRole(sysRoleAddDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @PutMapping(value = "/auth/updateSysRole")
    @Operation(summary = "修改角色")
    public Result updateSysRole(@RequestBody SysRoleAddOrUpdateDto sysRoleUpdateDto) {
        sysRoleService.updateSysRole(sysRoleUpdateDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @DeleteMapping(value = "/auth/deleteById/{roleId}")
    @Operation(summary = "删除角色")
    public Result deleteById(@PathVariable(value = "roleId") Long roleId) {
        sysRoleService.deleteById(roleId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping(value = "/auth/findAllRoles/{userId}")
    @Operation(summary = "查询所有角色和用户所拥有的角色")
    public Result<Map<String , Object>> findAllRoles(@PathVariable(value = "userId") Long userId) {
        Map<String, Object> resultMap = sysRoleService.findAllRoles(userId);
        return Result.build(resultMap , ResultCodeEnum.SUCCESS)  ;
    }
    
}